import { create } from 'zustand';
import type { App, AppSSO } from '@/types/app';
import type { IChatItem } from '@/app/components/base/chat/chat/type';

type State = {
  appDetail?: App & Partial<AppSSO>;
  appSidebarExpand: string;
  currentLogItem?: IChatItem;
  currentLogModalActiveTab: string;
  showPromptLogModal: boolean;
  showAgentLogModal: boolean;
  showMessageLogModal: boolean;
};

type Action = {
  setAppDetail: (appDetail?: App & Partial<AppSSO>) => void;
  setAppSiderbarExpand: (state: string) => void;
  setCurrentLogItem: (item?: IChatItem) => void;
  setCurrentLogModalActiveTab: (tab: string) => void;
  setShowPromptLogModal: (showPromptLogModal: boolean) => void;
  setShowAgentLogModal: (showAgentLogModal: boolean) => void;
  setShowMessageLogModal: (showMessageLogModal: boolean) => void;
};

export const useStore = create<State & Action>((set) => ({
  appDetail: {
    id: '436b0770-c521-4d8c-9f5e-624d9dd02ed5',
    name: 'Test',
    description: 'AAA',
    mode: 'workflow',
    icon_type: null,
    icon: '\ud83e\udd16',
    icon_background: '#FFEAD5',
    icon_url: null,
    enable_site: true,
    enable_api: true,
    model_config: null,
    workflow: {
      id: '465e7e56-8d9e-418b-992a-fa8dde5d9640',
      created_by: 'd12fea77-84dc-4590-9afc-9404d96aa842',
      created_at: 1727426939,
      updated_by: null,
      updated_at: null,
    },
    site: {
      access_token: 'dvBoqd2OkNB05rik',
      code: 'dvBoqd2OkNB05rik',
      title: 'Test',
      icon_type: null,
      icon: '\ud83e\udd16',
      icon_background: '#FFEAD5',
      icon_url: null,
      description: null,
      default_language: 'zh-Hans',
      chat_color_theme: null,
      chat_color_theme_inverted: false,
      customize_domain: null,
      copyright: null,
      privacy_policy: null,
      custom_disclaimer: null,
      customize_token_strategy: 'not_allow',
      prompt_public: false,
      app_base_url: 'https://udify.app',
      show_workflow_steps: true,
      use_icon_as_answer_icon: false,
      created_by: null,
      created_at: 1722589755,
      updated_by: null,
      updated_at: 1722589755,
    },
    api_base_url: 'https://api.dify.ai/v1',
    use_icon_as_answer_icon: false,
    created_by: null,
    created_at: 1722589755,
    updated_by: null,
    updated_at: 1722589755,
    deleted_tools: [],
  },
  setAppDetail: (appDetail) => set(() => ({ appDetail })),
  appSidebarExpand: '',
  setAppSiderbarExpand: (appSidebarExpand) => set(() => ({ appSidebarExpand })),
  currentLogItem: undefined,
  currentLogModalActiveTab: 'DETAIL',
  setCurrentLogItem: (currentLogItem) => set(() => ({ currentLogItem })),
  setCurrentLogModalActiveTab: (currentLogModalActiveTab) => set(() => ({ currentLogModalActiveTab })),
  showPromptLogModal: false,
  setShowPromptLogModal: (showPromptLogModal) => set(() => ({ showPromptLogModal })),
  showAgentLogModal: false,
  setShowAgentLogModal: (showAgentLogModal) => set(() => ({ showAgentLogModal })),
  showMessageLogModal: false,
  setShowMessageLogModal: (showMessageLogModal) =>
    set(() => {
      if (showMessageLogModal) {
        return { showMessageLogModal };
      } else {
        return {
          showMessageLogModal,
          currentLogModalActiveTab: 'DETAIL',
        };
      }
    }),
}));
